package hmis.pos.sys.service.Impl;

import hmis.pos.Base.BaseService.Impl.BaseServiceImpl;
import hmis.pos.sys.entity.SysUserEntity;
import hmis.pos.sys.repository.SysUserRepository;
import hmis.pos.sys.service.SysUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserEntity, SysUserRepository> implements SysUserService {

    @Autowired
    private SysUserRepository sysUserRepository;

    /**
     * 通过userName查找
     *
     * @param userName
     * @return
     */
    @Override
    public SysUserEntity findByUsername(String userName) {
        Optional<SysUserEntity> optional = sysUserRepository.findByUsername(userName);
        if (!optional.isPresent())
            return null;
        else
            return optional.get();
    }

    /**
     * 查找用户
     * @return
     */
    @Override
    public List<SysUserEntity> findAll() {
        return sysUserRepository.findAll();
    }

    /**
     * 添加一个用户
     *
     * @param sysUserEntity
     * @return
     */
    @Override
    public SysUserEntity save(SysUserEntity sysUserEntity) {
        Optional<SysUserEntity> optional = sysUserRepository.findByUsername(sysUserEntity.getUsername());
        if (optional.isPresent())
            return null;
        return sysUserRepository.save(sysUserEntity);
    }

}
